package com.cbn.ibcp.web.jishiting.operator.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.cbn.ibcp.common.core.controller.BaseController;
import com.cbn.ibcp.common.core.domain.PageResult;
import com.cbn.ibcp.common.core.page.TableDataInfo;
import com.cbn.ibcp.jishiting.media.bo.EpisodeResourceStreamBO;
import com.cbn.ibcp.jishiting.media.service.IEpisodeResourceStreamService;
import com.cbn.ibcp.jishiting.rms.service.IResBaseService;
import com.cbn.ibcp.web.jishiting.operator.convert.EpisodeResourceStreamBOConvert;
import com.cbn.ibcp.web.jishiting.operator.convert.EpisodeResourceStreamDTOConvert;
import com.cbn.ibcp.web.jishiting.operator.dto.EpisodeResourceStreamDTO;
import com.cbn.ibcp.web.jishiting.operator.vo.EpisodeResourceStreamVO;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/episodeResourceStream")
@RequiredArgsConstructor
public class EpisodeResourceStreamController extends BaseController {

    private final IEpisodeResourceStreamService episodeResourceStreamService;

    @PreAuthorize("@ss.hasPermi('jisiting:media:resourcestream:list')")
    @GetMapping("/selectWithEpisodeListPage")
    public PageResult<EpisodeResourceStreamVO> selectWithEpisodeList(EpisodeResourceStreamDTO episodeResourceStreamDTO) {
        boolean paramIsNull = Objects.isNull(episodeResourceStreamDTO.getAlbumId()) || Objects.isNull(episodeResourceStreamDTO.getEpisodeId()) || Objects.isNull(episodeResourceStreamDTO.getSourceId()) || StrUtil.isBlank(episodeResourceStreamDTO.getAlbumName()) || StrUtil.isBlank(episodeResourceStreamDTO.getEpisodeName()) || StrUtil.isBlank(episodeResourceStreamDTO.getCpCode());
        EpisodeResourceStreamBO episodeResourceStreamBO = EpisodeResourceStreamDTOConvert.convertTOEpisodeResourceStreamBO(episodeResourceStreamDTO);
        startPage();
        List<EpisodeResourceStreamBO> episodeResourceStreamBOList = episodeResourceStreamService.selectWithEpisodeList(episodeResourceStreamBO);
        TableDataInfo dataTable = getDataTable(episodeResourceStreamBOList);
        if (CollectionUtil.isEmpty(episodeResourceStreamBOList)) {
            return PageResult.ok(dataTable.getTotal(), null);
        }
        List<EpisodeResourceStreamVO> episodeResourceStreamVOList = episodeResourceStreamBOList.stream().map(EpisodeResourceStreamBOConvert::convertToEpisodeResourceStreamVO).collect(Collectors.toList());
        return PageResult.ok(dataTable.getTotal(), episodeResourceStreamVOList);
    }
}
